﻿<UserControl x:Class="Demos.ShortestPaths.Dijkstra.ArbolUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Name="arbol"
             xmlns:Local="clr-namespace:Demos.ShortestPaths.Dijkstra"
             d:DesignHeight="300" d:DesignWidth="300">
    
    <UserControl.Resources>
        <Local:PuntaFlechaConverter x:Key="myConverter"/>

        <DataTemplate DataType="{x:Type Local:VerticeVisual}">
            <Grid MouseLeftButtonDown="VerticeMouseLeftButtonDown">
                <Label Visibility="Hidden" Content="{Binding Identificador}"></Label>
                <Ellipse Height="{Binding Altura}" Width="{Binding Anchura}"
                         Stroke="Black" StrokeThickness="2" Fill="{Binding Color}"/>
                <Label HorizontalContentAlignment="Center" Content="{Binding Etiqueta}"
                       Height="{Binding Altura}" Width="{Binding Anchura}"
                       MouseDoubleClick="EtiquetaMouseDoubleClick"/>
            </Grid>
        </DataTemplate>

        <DataTemplate DataType="{x:Type Local:AristaVisual}">
            <Grid MouseLeftButtonDown="AristaMouseLeftButtonDown">
                <Label Visibility="Hidden" Content="{Binding Identificador}"></Label>
                <Line StrokeThickness="2" StrokeEndLineCap="Triangle"
                  X1="{Binding X1}" Y1="{Binding Y1}"
                  X2="{Binding X2}" Y2="{Binding Y2}"
                  Stroke="{Binding Color}" Name="raya">
                </Line>
                <Canvas>
                    <Ellipse Height="10" Width="10" Fill="{Binding Color}"
                             Canvas.Left="{Binding X2, Converter={StaticResource myConverter}}" 
                             Canvas.Top="{Binding Y2, Converter={StaticResource myConverter}}">
                    </Ellipse>
                </Canvas>
                
                <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                       Content="{Binding Etiqueta}"
                       MouseDoubleClick="EtiquetaMouseDoubleClick"/>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    
    <DockPanel>
        <GroupBox Header="Mapa" DockPanel.Dock="Top" Margin="5">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" VerticalContentAlignment="Center" Margin="5">Num. vértices:</Label>
                <TextBox Grid.Row="0" Grid.Column="1" Name="numVerticesTxt" Margin="5" TextAlignment="Right"
                         Text="{Binding NumeroVertices, UpdateSourceTrigger=PropertyChanged}"/>
                <Label Grid.Row="1" Grid.Column="0" VerticalContentAlignment="Center" Margin="5">Max. aristas:</Label>
                <TextBox Grid.Row="1" Grid.Column="1" Name="maxAristasTxt" Margin="5" TextAlignment="Right"
                         Text="{Binding MaximasAristas, UpdateSourceTrigger=PropertyChanged}"/>
                <Button Grid.Row="0" Grid.Column="2" Margin="5" 
                        Command="{Binding CrearMapaAleatorioCommand}">Aleatorio</Button>
                <Button Grid.Row="0" Grid.Column="4" Margin="5" 
                        Command="{Binding OcultarPesosCommand}">Ocultar Pesos</Button>
                <Button Grid.Row="0" Grid.Column="3" Margin="5"
                        Content="{Binding ContenidoBoton}"
                        Command="{Binding AñadirVerticesCommand}"></Button>
                <Button Grid.Row="1" Grid.Column="3" Margin="5"
                        Command="{Binding CrearNuevaAristaCommand}">Nueva arista</Button>
                <Button Grid.Row="1" Grid.Column="2" Margin="5" 
                        Command="{Binding BorrarElementoCommand}">Borrar elemento</Button>
                <Button Grid.Row="1" Grid.Column="4" Margin="5" 
                        Command="{Binding BorrarMapaCommand}">Borrar todo</Button>
            </Grid>
        </GroupBox>

        <Border DockPanel.Dock="Left" BorderBrush="LightGray" BorderThickness="1" Margin="5">
            <ItemsControl ItemsSource="{Binding ElementName=arbol, Path=ContenidoVisual}" Name="control"
                          SizeChanged="controlSizeChanged">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Name="mapaCv" Background="Transparent" MouseLeftButtonDown="mapaCvMouseLeftButtonDown"></Canvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Canvas.Left" Value="{Binding X}" />
                        <Setter Property="Canvas.Top" Value="{Binding Y}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </Border>
    </DockPanel>
</UserControl>
